package com.vitabuybuy.tv.ui.mine

import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.core.os.bundleOf
import com.vitabuybuy.tv.R
import com.vitabuybuy.tv.databinding.ActivityNicknameBinding
import com.library.common.base.BaseActivity
import com.vitabuybuy.tv.ui.viewmodel.NoViewModel
import com.library.common.toast.XToast
import kotlinx.android.synthetic.main.activity_nickname.*

class UpdateNicknameActivity : BaseActivity<NoViewModel, ActivityNicknameBinding>() {
    override val layoutId: Int
        get() = R.layout.activity_nickname

    private var content: String? = null

    override val isUseBaseTitleBar: Boolean
        get() = true

    override fun onGetBundle(bundle: Bundle?) {
        content = bundle?.getString("content")
    }

    override fun initView(savedInstanceState: Bundle?) {
        setTitle(getString(R.string.setting_nicknmae))
        mBinding.content = content
        mTvRight.apply {
            text = getString(R.string.save)
            visibility = View.VISIBLE
            setOnClickListener {
                when {
                    et_content.text.isEmpty() -> XToast.normal(getString(R.string.please_input_nickname))
                    et_content.text.length < 4 || et_content.text.length > 20 -> XToast.normal(
                        getString(R.string.nickname_dialog_tip)
                    )
                    else -> {
                        val intent = Intent()
                        intent.putExtras(bundleOf(Pair("nickname", et_content.text.toString())))
                        finishResult(intent)
                    }
                }
            }
        }
    }

    companion object {
        fun openActivity(mContext: BaseActivity<*, *>, content: String?, requestCode: Int) {
            mContext.startForResult(
                bundleOf(Pair("content", content)),
                requestCode,
                UpdateNicknameActivity::class.java
            )
        }
    }
}
